import { Vec2, v2 } from "cc";
import { UserInfo } from "../common/UserInfo";
import { EventType } from "../settings/const";
import { MapZone } from "./MapZone";
import { Soldier } from "./Soldier";
import { Village } from "./Village";
/**
 * 军队类型
 */
export enum ArmyType {
    /**增援 */
    Help = 1,
    /**侦察 */
    Detection = 2,
    /**普通 */
    Attack = 3,
    /**抢夺 */
    Rob = 4,
}
/**
 * 军队类型，服务器需要的值 2增援 3普通/侦察 4抢夺
 */
export const ArmyTypeValue = {
    [ArmyType.Help]: 2,
    [ArmyType.Detection]: 3,
    [ArmyType.Attack]: 3,
    [ArmyType.Rob]: 4,
}
/**
 *间谍类型，侦察目标 1经济 2军事
 */
export enum SpyType {
    Economy = 1,
    Army = 2,
}

export enum AttackMode {
    /**0攻击 */
    Attack = 0,
    /**1抢劫 */
    Rob = 1,
    /**2侦查 */
    Detection = 2,
    /**3投石单砸 */
    OneCatapult = 3,
    /**4投石双砸 */
    TwoCatapult = 4,
    /**5建立新村庄 */
    NewVillage = 5,
    /**6攻击绿洲 */
    AttackOasis = 6,
    /**7抢劫绿洲 */
    RobOasis = 7,
    /**8游侠侦查 */
    HeroDetection = 8,
    /**9游侠暗杀 */
    HeroKill = 9,
    /**10游侠破坏 */
    HeroDestroy = 10,
    /**11商人速建 */
    TraderBuild = 11,

}
export const AttackModeCh = {
    [AttackMode.Attack]: '攻击',
    [AttackMode.Rob]: '抢夺',
    [AttackMode.Detection]: '侦查',
    [AttackMode.OneCatapult]: '投石单砸',
    [AttackMode.TwoCatapult]: '投石双砸',
    [AttackMode.NewVillage]: '建立新村庄',
    [AttackMode.AttackOasis]: '攻击绿洲',
    [AttackMode.RobOasis]: '抢夺绿洲',
    [AttackMode.HeroDetection]: '游侠侦查',
    [AttackMode.HeroKill]: '游侠暗杀',
    [AttackMode.HeroDestroy]: '游侠破坏',
    [AttackMode.TraderBuild]: '商人速建',

}
/**军队状态 */
export enum ArmyState {
    /** 0 驻守模式 */
    Stay = 0,
    /**1 攻击途中 */
    Attack = 1,
    /** 2 支援途中 */
    Help = 2,
    /** 3 返回途中 */
    Back = 3,
}
/**
 * 军队
 */
export class Army {
    armyId: number
    /**攻击模式 */
    eAttackMode: AttackMode
    eHandle: number
    eParam: string
    /**事件类型  EventType from const.js */
    eType: number
    /**军队到达时间 */
    endTime: number
    /**军队出发时间 */
    startTime:number
    /**种族类型 */
    race: number
    /**终点 */
    simZoneId: number
    simZoneName: string
    soldiersString: string
    state: ArmyState
    uid: number
    /**起点 */
    zoneId: number
    zoneName: string
    /**士兵列表 */
    soldierList: Soldier[] = []
    /**间谍侦察类型 */
    spyType: SpyType
    /**军队类型 */
    armyType: ArmyType

    static createArmy(list: Soldier[]): Army {
        let army = new Army()
        army.soldierList = list

        return army
    }

    static createArmyByData(data: Army): Army {
        let army = new Army()
        for(let key in data){
            army[key] = data[key]
        }
        
        army.soldierList = Army.createSoldierList(army.soldiersString)
        return army
    }
    /**根据 soldiersString(id,count;id,count...) 来创建军队*/
    static createArmyBySoldiersString(str: string):Army{
        let army = new Army()
        army.soldierList = Army.createSoldierList(str)
        return army
    }

    static createSoldierList(str: string): Soldier[] {
        let soldierList: Soldier[] = []
        let list = str.split(';')
        for (let item of list) {
            let arr = item.split(',')
            let id = parseInt(arr[0])
            let count = parseInt(arr[1])
            let so = Soldier.createById(id)
            so.count = count
            soldierList.push(so)
        }
        return soldierList
    }

    get consume(): number {
        let count = 0
        for (let so of this.soldierList) {
            count += so.consume * so.count
        }
        return count
    }
    get speed():number{
        let s = 10000
        for (let so of this.soldierList) {
            if(so.armyCount > 0 && so.speed < s){
                
                s = so.speed
            }
        }
        
        return s
    }

    get tittle(): string {
        if (this.state == ArmyState.Stay) {
            if (this.zoneId == this.simZoneId) {
                return '我的驻村军队'
            }

            if (this.zoneId == UserInfo.instance.villageData.zoneId) {
                return `我对<color=#FAC86D>${this.simZoneName}</color>的支援`
            } else {
                return `<color=#FAC86D>${this.zoneName}</color>对我的支援`
            }
        }
        if (this.state == ArmyState.Attack) {
            if (this.zoneId == UserInfo.instance.villageData.zoneId) {
                return `我对<color=#FAC86D>${this.simZoneName}</color>的${AttackModeCh[this.eAttackMode]}`
            } else {
                return `<color=#FAC86D>${this.zoneName}</color>对我的${AttackModeCh[this.eAttackMode]}`
            }
        }
        if (this.state == ArmyState.Help) {
            if (this.zoneId == UserInfo.instance.villageData.zoneId) {
                return `我对<color=#FAC86D>${this.simZoneName}</color>的支援`
            } else {
                return `<color=#FAC86D>${this.zoneName}</color>对我的支援`
            }
        }
        if (this.state == ArmyState.Back) {
            if (this.zoneId == UserInfo.instance.villageData.zoneId) {
                return `我从<color=#FAC86D>${this.simZoneName}</color>的返回`
            } else {
                return `<color=#FAC86D>${this.zoneName}</color>返回去了`
            }

        }

    }

    get soldierCount(): number {
        let count = 0
        if (this.armyType == ArmyType.Help) {
            for (let soldier of this.soldierList) {
                count += soldier.armyCount
            }
        }

        if (this.armyType == ArmyType.Detection) {
            for (let soldier of this.soldierList) {
                if (soldier.isScout) {
                    count += soldier.armyCount
                }
            }
        }
        if (this.armyType == ArmyType.Attack || this.armyType == ArmyType.Rob) {
            for (let soldier of this.soldierList) {
                if (!soldier.isScout) {
                    count += soldier.armyCount
                }
            }
        }
        return count
    }

    get armyTypeValue(): number {
        return ArmyTypeValue[this.armyType]
    }

    get catapultCount(): number {

        return this.soldierList[7].armyCount
    }

    get params(): any {
        let p = {
            c: this.armyTypeValue,
        }

        for (let i = 0; i < this.soldierList.length; i++) {
            let so = this.soldierList[i]
            let index = i + 1

            if (this.armyType == ArmyType.Help) {
                p['t' + index] = so.armyCount
            }

            if (this.armyType == ArmyType.Detection) {
                if (so.isScout) {
                    p['t' + index] = so.armyCount
                }
            }

            if (this.armyType == ArmyType.Attack || this.armyType == ArmyType.Rob) {
                if (!so.isScout) {
                    p['t' + index] = so.armyCount
                }
            }
        }



        if (this.armyType == ArmyType.Detection) {
            p['spy'] = this.spyType
        }


        return p
    }

    /**
     * 军队移动到该村庄需要多少时间
     * @param village 
     * @returns 
     */
    moveToVillageTime(village:Village):number{
        let x = UserInfo.instance.villageData.xId
        let y = UserInfo.instance.villageData.yId
        let myPos = v2(x, y)
        let targetPos = v2(village.x, village.y)
        let distance = Vec2.distance(myPos, targetPos)
        let time = (distance / this.speed) * 3600
        time = time / UserInfo.instance.speed
        return Math.floor(time)
    }

    /**
     * 计算军队两点间移动需要的时间
     * @param z1 坐标 zoneId
     * @param z2 坐标 zoneId
     * @param sph 速度加成
     * @param sph_basic 基础速度
     * @returns 
     */
    moveTime(z1, z2, sph, sph_basic) {
        if (sph_basic == 0) {
            sph_basic = 3;
        }
        
        let distance = MapZone.distance(z1, z2);

        let ret = 0;
        let baseDistance = 30.0 / UserInfo.instance.speed

        if (distance > baseDistance) {
            ret = Math.max(1, 3600 * ((distance - baseDistance) / sph + baseDistance / sph)) | 0;
        } else {
            ret = Math.max(1, 3600 * (distance / sph_basic)) | 0;
        }
        return ret;
    }
} 